using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Drawing;

using TreeSharp;

using Styx;
using Styx.Logic;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.Helpers;

namespace FelMaster.Classes.Mage
{
    class EverydayFire : TalentBase
    {
        public override string Name
        {
            get { return "Everyday Fire V1.22"; }
        }

        public override string KeySpell
        {
            get { return "Pyroblast"; }
        }

        public List<WoWUnit> AOECHK(WoWPoint chkloc, double radius)
        {
            List<WoWUnit> hostile = ObjectManager.GetObjectsOfType<WoWUnit>(true, false);
            List<WoWUnit> avoid = new List<WoWUnit>();
            var MaxDistance2 = radius * radius;
            try
            {
                hostile = hostile.Where(x =>
                    x.IsAlive &&
                    (x.IsTargetingMeOrPet || x.IsTargetingMyPartyMember || x.IsTargetingMyRaidMember || x.IsTargetingPet) &&
                    !x.IsCritter &&
                    !x.IsPet &&
                    x.Location.Distance2DSqr(chkloc) < MaxDistance2
                ).ToList();
                avoid = hostile.Where(x =>
                    //check for controlled units, like sheep etc
                SimCraftBase.Instance.UnitIsControlled(x, true)
                ).ToList();
                if (avoid != null && avoid.Count > 0)
                {
                    hostile = null;
                    hostile.Add(Me.CurrentTarget);
                }
            }
            catch { }
            return hostile;
        }

        public override Composite SingleRotation
        {
            get {
                return new PrioritySelector(
                    new Decorator(x => SC.PlayerIsChanneling, new TreeSharp.Action(a => SC.Log("Channeling"))),
                    SC.UseEngineerGloves(a => true, "Engineer's glove enchant"),
                    SC.UseBagItem("Mana Gem", a => Me.ManaPercent < 80, "Using Mana Gem"),
                    SC.CastSpell("Molten Armor", a => Me.ManaPercent > 40 && !SC.PlayerHasBuff("Molten Armor"), "Dont Need Mana - Switching To Molten Armor"),
                    SC.CastSpell("Berserking", a => ((Me.CurrentTarget.Level == 87 && Me.CurrentTarget.Elite) || (Me.CurrentTarget.Level == 88)) && SC.TargetHasDebuff("Living Bomb") && SC.TargetHasDebuff("Ignite") && (SC.TargetHasDebuff("Pyroblast") || SC.TargetHasDebuff("Pyroblast!")), "Berserking"),
                    SC.CastSpell("Combustion", a => ((Me.CurrentTarget.Level == 87 && Me.CurrentTarget.Elite) || (Me.CurrentTarget.Level == 88)) && SC.TargetHasDebuff("Living Bomb") && SC.TargetHasDebuff("Ignite") && (SC.TargetHasDebuff("Pyroblast") || SC.TargetHasDebuff("Pyroblast!")), "Combustion"),
                    SC.CastSpell("Mirror Image", a => ((Me.CurrentTarget.Level == 87 && Me.CurrentTarget.Elite) || (Me.CurrentTarget.Level == 88)), "Mirror Image"),
                    SC.CastSpell("Living Bomb", a => !SC.TargetHasDebuff("Living Bomb"), "Living Bomb"),
                    SC.RunMacroText("/Cast Pyroblast", a => SC.PlayerHasBuff ("Hot Streak"), "Hot Streak"),
                    SC.CastSpell("Flame Orb", a => true, "Flame Orb"),
                    SC.RunMacroText("/Cast Scorch", a => Me.IsMoving, "Scorch"),
                    SC.CastSpell("Fire Blast", a => AOECHK(Me.CurrentTarget.Location, 12).Count >= 2 && (SC.TargetHasDebuff("Living Bomb") || SC.TargetHasDebuff("Combustion")), "Impact"),
                    SC.CastSpell("Flamestrike", a => AOECHK(Me.CurrentTarget.Location, 6).Count >= 6, "Flamestrike"),
                    SC.CastSpell("Fireball", a => true, "Fireball"),
                    SC.CastSpell("Mage Armor", a => !SC.PlayerHasBuff("Mage Armor") && Me.ManaPercent < 5, "Need Mana - Switching To Mage Armor"),
                    SC.CastSpell("Scorch", a => true, "Scorch")
                );
            }
        }

        public override Composite Buffs
        {
            get
            {
                return new PrioritySelector();
            }
        }

        public override Composite PVPRotation
        {
            get { throw new NotImplementedException(); }
        }

        public override Composite PVERotation
        {
            get
            {
                return SingleRotation;
            }
        }
    }
}